﻿#region Imports
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Forms;
using CommonUtilities;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
#endregion

namespace BulkRenamer.Util
{
    /// <summary>
    /// Value Wrapper for binding against IEnumerable of structs
    /// <para>e.g.: Use 'BulkRenamer.Util.Utilities.ConvertToSingletonValueWrapper(IEnumerable<object> orgCollection)' for conversion</para>
    /// </summary>
    public class SingletonValueWrapper
    {
        #region Properties
        public string SingletonValue { get; set; }
        #endregion

        #region Methods
        /// <summary>
        /// Converts a collection of structs into a collection of a class 
        /// having the value of the struct assigned to the singleton property for binding to a control
        /// </summary>
        /// <param name="orgCollection">Original collection</param>
        /// <returns>A collection of SingletonValueWrapper with the value of the structs assigned to the singleton property</returns>
        public static IEnumerable<SingletonValueWrapper> ConvertToSingletonValueWrapper(IEnumerable<object> orgCollection)
        {
            IEnumerable<SingletonValueWrapper> finalColl = new ObservableCollection<SingletonValueWrapper>();
            if (orgCollection.IsLinqIterable<object>(true))
                finalColl = orgCollection.Distinct().Select(logdet => new SingletonValueWrapper { SingletonValue = logdet.ToString() }).AsEnumerable<SingletonValueWrapper>();

            return finalColl;
        }

        /// <summary>
        /// Binds data to a list-based control (ListBox/ComboBox)
        /// </summary>
        /// <param name="orgCollection">Original collection</param>
        /// <param name="control">Control to bind to</param>
        public static void BindToControl(IEnumerable<SingletonValueWrapper> orgCollection, ListControl control)
        {
            control.DataSource = orgCollection;
            control.DisplayMember = control.ValueMember = "SingletonValue";
        }

        public override string ToString()
        {
            return Utilities.ToString(this);
        }
        #endregion
    }
}
